

#include <stdio.h>
#include <string.h>
#include <string>

#include "pcre_wrapper.h"

#define OVECCOUNT 30    /* should be a multiple of 3 */

int main(int argc, char **argv) {

	char *pattern = argv[1];
	int *re = pcre_new_express(pattern);
	if (re == 0) {
		printf("PCRE compilation NULL\n");
		return 1;
	}

	std::string bufs = "";
	int line = 0;
	char lbuf[256];
	while (fgets(lbuf, sizeof(lbuf), stdin)) {
		lbuf[strlen(lbuf) - 1] = 0;
		bufs += lbuf;
	}
	char *data = (char*) bufs.c_str();
	int dataSize = (int) strlen(data);

	int ovector[OVECCOUNT];
	memset(ovector, -1, sizeof(ovector));
	int offset = 0,retStart = 0, retLen = 0;
	int imatch = 0;
	for (;;) {
		int rt = pcre_exc_express(re, data, dataSize, offset, &retStart,
				&retLen);
		if (rt == -1) break;
		else {
			offset = retStart + retLen;
			char *substring_start = data + retStart;
			printf("[%3d]Match==>\033[1;31m%.*s\033[0m\n", imatch, retLen,
					substring_start);
			imatch++;
		}
	}
	pcre_free_express(re);
	return 0;

}

/* End of pcredemo.c */
